package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.assets.passenger.model.dto.PassengerMessageDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerMessagePageParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerMessageParam;
import com.yungu.swift.assets.passenger.service.PassengerMessageService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : cuixiuyin
 * @date : 2019/6/24
 */
@RestController
@RequestMapping("/admin/v1/operation/passenger/message")
@Api(value = "【运营管理】", tags = "乘客消息管理")
public class PassengerMessageAdmin {

    @Reference
    private PassengerMessageService passengerMessageService;

    @ResponseImgOverride
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "乘客消息管理分页查询", httpMethod = "POST")
    public ResponseData<PageVo<PassengerMessageDto>> queryPage(@RequestBody AdminPassengerMessagePageParam
                                                                       passengerMessagePageParam) {
        return passengerMessageService.queryPage(passengerMessagePageParam);
    }

    @ResponseImgOverride
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "乘客消息详情", httpMethod = "GET")
    public ResponseData<PassengerMessageDto> detail(@RequestParam String uuid) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerMessageService.get(uuid);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "乘客消息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminPassengerMessageParam passengerMessageParam) {
        PassengerMessageDto messageDto = new PassengerMessageDto();
        BeanUtils.copyProperties(passengerMessageParam, messageDto);
        if (StringUtils.isEmpty(messageDto.getUuid())) {
            messageDto.setUuid(StringUtils.buildUUID());
            messageDto.setCreateOn(new Date());
            messageDto.setCreateBy(passengerMessageParam.getUserUuid());
            messageDto.setSendTime(new Date());
            messageDto.setType(1);
            return passengerMessageService.add(messageDto);
        }
        messageDto.setUpdateOn(new Date());
        messageDto.setUpdateBy(passengerMessageParam.getUserUuid());
        return passengerMessageService.edit(messageDto);
    }

}
